package com.huazhi.wzsnw.service.impl;

import com.huazhi.wzsnw.mapper.DossierMapper;
import com.huazhi.wzsnw.model.Dossier;
import com.huazhi.wzsnw.service.DossierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class DossierServiceImpl implements DossierService {

    @Autowired
    private DossierMapper dossierMapper;

    @Override
    public int save(Dossier record) {
        return dossierMapper.insertSelective(record);
    }

    @Override
    public int delete(Dossier record) {
        return dossierMapper.deleteByPrimaryKey(record.getTid());
    }

    @Override
    public int deletes(List<Dossier> records) {
        for (Dossier dossier:records){
            delete(dossier);
        }
        return 0;
    }

    @Override
    public int update(Dossier record) {
        return dossierMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public Dossier findByTid(String tid) {
        return dossierMapper.selectByPrimaryKey(tid);
    }

    @Override
    public List<Dossier> findPagingByListTid(List<String> stringList, Integer pageIndex, Integer pageSize) {
        return dossierMapper.findPagingByListTid(stringList, pageIndex, pageSize);
    }

    @Override
    public List<String> getSearchDossierList(String search, Dossier dossier) {
        return dossierMapper.getSearchDossierList(search, dossier);
    }

    @Override
    public List<String> getSearchDossierListByRequest(HttpServletRequest request) throws ParseException {
        String search = request.getParameter("key");
        String check = request.getParameter("selectdata");
        String state = request.getParameter("state");
        String start = request.getParameter("starttime");
        String end = request.getParameter("endtime");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Dossier dossier = new Dossier();
        List<String> list = new ArrayList<String>();
        if(check != null && !check.isEmpty()){
            String[] ids = check.split(",");
            if(ids.length > 0){
                list.addAll(Arrays.asList(ids));
            }
        }else {
            if(start != null && !"".equals(start)){
                dossier.setStarttime(sdf.parse(start));
            }
            if(end != null && !"".equals(end)){
                dossier.setEndtime(sdf.parse(end));
            }
            List<String> params = new ArrayList<String>();
            if (search == null) { search = ""; }
            if ("".equals(search)) {
                params.add("");
            } else if (search.contains(" ")) {
                params = Arrays.asList(search.split(" "));
            } else if (!search.contains(" ")) {
                params.add(search);
            }
            boolean firstSearch = true;
            for (String oneParam : params) {
                if (firstSearch) {
                    list = getSearchDossierList(oneParam,dossier);
                    firstSearch = false;
                } else {
                    List<String> middleList = getSearchDossierList(oneParam,dossier);
                    list.retainAll(middleList);
                }
            }
        }
        return list;
    }
}
